<template>
  <div class="smart-query-form">
    <a-tabs v-model:activeKey="activeKey" @change="changeTab">
      <a-tab-pane key="1" v-privilege="'productionPlanPosition:query'" forceRender tab="站位表">
        <production-plan-position-list ref="planPositionRef" />
      </a-tab-pane>
      <a-tab-pane key="2" v-privilege="'loadMaterialCheckLog:query'" forceRender tab="上料记录">
        <load-material-check-log-list ref="loadMaterialRef2" />
      </a-tab-pane>
      <a-tab-pane key="3" v-privilege="'loadMaterialCheckLog:query:ipqc'" forceRender tab="IPQC巡检记录">
        <load-material-check-log-list ref="loadMaterialRef3" />
      </a-tab-pane>
    </a-tabs>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import ProductionPlanPositionList from './components/production-plan-position-list.vue';
  import LoadMaterialCheckLogList from './components/load-material-check-log-list.vue';

  const activeKey = ref('1'); // 直接设置默认值
  const planInfoRef = ref();
  const planPositionRef = ref();
  const loadMaterialRef2 = ref(); // 上料记录
  const loadMaterialRef3 = ref(); // IPQC巡检记录

  function setPlanInfo(planInfo) {
    planInfoRef.value = planInfo;
    // 主动触发一次数据更新
    updateChildComponents();
  }

  function updateChildComponents() {
    if (!planInfoRef.value) return;

    switch (activeKey.value) {
      case '1':
        planPositionRef.value?.setPlanInfo?.(planInfoRef.value);
        break;
      case '2':
        loadMaterialRef2.value?.setPlanInfo?.(planInfoRef.value, false);
        break;
      case '3':
        loadMaterialRef3.value?.setPlanInfo?.(planInfoRef.value, true);
        break;
    }
  }

  function changeTab(e) {
    activeKey.value = e;
    updateChildComponents();
  }

  defineExpose({
    setPlanInfo,
  });
</script>
